package com.hd.hdxgddcommon.utils;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2019/4/30.
 */
public class DataItemCopyUtils {

    public final static String REG = "^[aA][1-9]\\d*$";

    public static void setItem(Object o, Map<String, String> data) {
        if (data != null) {
            Set<String> keys = data.keySet();
            Iterator<String> iterator = keys.iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                if (StringUtils.isNotBlank(key)) {
                    if (Pattern.matches(REG, key)) {
                        Class<?> aClass = o.getClass();
                        try {
                            String lowKey = key.toLowerCase();
                            Field field = aClass.getDeclaredField(lowKey);
                            field.setAccessible(true);
                            field.set(o, data.get(key));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
